home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8121 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.5 KB

  1. Path: gecm.com!usenet
  2. From: Colin Moore <Colin.Moore@gecm.com>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Hiding a password
  5. Date: 1 Mar 1996 17:49:02 GMT
  6. Organization: GEC-Marconi
  7. Message-ID: <4h7dae$cs2@gcsin3.geccs.gecm.com>
  8. References: <1996Feb29.224936.137160@forest>
  9. NNTP-Posting-Host: isd62d.rochstr.gmav.gecm.com
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 1.1 (Windows; I; 16bit)
  14.  
  15. ebromber@forest.drew.edu wrote:
  16. >I recently wrote a password program. However, there is one little flaw I 
  17. >want to fix. When the password is entered, it is visible on the screen. I 
  18. >was wondering if anyone knows how to hide the password by  printing 
  19. >asterisks instead of the actual character. 
  20. >Thanks in advance.
  21. >
  22. >ebromber@drew.edu
  23. >
  24.  
  25.  
  26. RE: hiding a password
  27.  
  28. I dont know if you are using a PC or unix system....
  29.  
  30. On a Pc....
  31.    you need to create a loop that reads the keyboard one keypress at a 
  32. time without echo (i.e. 'getch()') then check the inputs for carrage 
  33. return ('\n') if it isn't then place the input into a string array and 
  34. print a star on the screen. if the input is a carrage return then quit 
  35. the loop and place an end of string marker on the end of the string array 
  36. ('\0').
  37.  
  38. char ch = '\0';
  39. char sting[100];
  40. int string_position = 0;
  41.  
  42. while ((ch = getch()) != '\n') {
  43.   putch('*');
  44.   string[string_position++] = ch;
  45. }
  46. string[string_position] = '\0';
  47.  
  48.  
  49. this can easily be adapted from there to include the use of backspace 
  50. ('\b') by checking for it like the carrage return taking the last input 
  51. back out of the string array and removing the last star(print a space 
  52. over it)
  53.  
  54.  
  55. char ch = '\0';
  56. char sting[10];
  57. int string_position = 0;
  58.  
  59. while ((ch = getch()) != '\n') {
  60.                                    /* check for backspace */
  61.   if ((ch == '\b') && (string_position > 0)) {
  62.     printf("\b \b);     /* remove star */
  63.     string_position--;  /* remove last input */
  64.   }
  65.  
  66.   if (string_position > 8) { /* an string overflow check */
  67.     putch('*');
  68.     string[string_position++] = ch;
  69.   }
  70. }
  71. string[string_position] = '\0';
  72.  
  73.  
  74. 'getch()' does tend to stop ctrl+c from working as well..
  75.  
  76. As for unix (it been a long time since I used it but) :-
  77. simular to the pc version but using 'getchar()' insted of 'getch()'
  78. and turn the input mode of the terminal to raw getting the input and then 
  79. back to normal afterwards (check out tty.h file or terminal escape codes)
  80. or there may already be a function pervided as I said it was a long time 
  81. ago for me...hope this helps in some way..
  82.  
  83.